package com.uh.checking.proc;

import com.uh.common.utils.CommandUtils;
import com.uh.common.utils.OSType;
import com.uh.checking.config.ConfigException;
import com.uh.checking.config.JarConfig;

import java.io.File;

import static com.uh.checking.proc.JarCheckingUtils.execute;
import static com.uh.checking.config.JarCheckingConstants.CMD_RM;

public class MainJarProcessor {

    private JarConfig config;

    private File srcJar; //源Jar文件

    private String srcJarName; ////源Jar文件(文件名)

    private File unzipDir; //解开后的目录名（srcJar所在的目录下）

    public MainJarProcessor(JarConfig config) {
        this.config = config;
        File wdir = this.config.getWorkingDirFile();
        this.srcJar = new File(wdir, config.getSrcJar());
        System.out.println("MainJarProcessor begin! Working Directory:" + wdir.getAbsolutePath());

        if(srcJar.exists() == false) {
            throw new ConfigException("srcJar not exists: " + config.getSrcJar());
        }
        this.srcJarName = this.srcJar.getName();

        this.unzipDir  = new File(wdir, config.getUnzipDir());
        if(this.unzipDir.exists() == false) {
            this.unzipDir.mkdirs();
        }
    }

    public File getUnzipDir() {
        return unzipDir;
    }

    public JarConfig getConfig() {
        return config;
    }

    public void clean() {
        if(unzipDir.exists() && unzipDir.isDirectory()) {
            String[] files = unzipDir.list();
            for (String file : files) {
                File rmFile = new File(unzipDir, file);
                if(rmFile.exists()) {
                    if(rmFile.isDirectory()) {
                        execute(CMD_RM + rmFile.getPath());
                    }
                    else {
                        rmFile.delete();
                    }
                }
            }
        }
    }

    public void unzipMainJar() {
        if(config.getSrcJarType() == null) {
            throw new ConfigException("Failed to get srcJar type! srcJar:" + config.getSrcJar());
        }

        switch (config.getSrcJarType()) {
            case JAR:
                execute("cd " + unzipDir.getPath() + " && jar -xvf ../" + srcJarName);
                break;
            case TAR_GZ:
                execute("cd " + unzipDir.getPath() + " && tar -zxf ../" + srcJarName);
                break;
            case ZIP:
                execute("cd " + unzipDir.getPath() + " && unzip ../" + srcJarName);
                break;
        }

    }

    public void zipMainJar() {
        StringBuffer buf = new StringBuffer();
        String[] subList = unzipDir.list();
        for(String subName : subList) {
            File f = new File(unzipDir, subName);
            if(f.isHidden() == false) {
                if (f.isDirectory()) {
                    buf.append(' ').append(subName).append('/');
                } else { //除隐藏文件外的文件进行打包
                    buf.append(' ').append(subName);
                }
            }
        }

        String TAR = (CommandUtils.getOSType() == OSType.MAC) ? "gtar" : "tar";


        switch (config.getSrcJarType()) {
            case JAR:
                execute("cd " + unzipDir.getPath() + " && jar -cvfM0 ../" + config.getTargetJar() + buf);
                break;
            case TAR_GZ:
                execute("cd " + unzipDir.getPath() + " && " + TAR + " -zcf ../" + config.getTargetJar() + buf);
                break;
            case ZIP:
                execute("cd " + unzipDir.getPath() + " && zip ../" + config.getTargetJar() + buf);
                break;
        }

        //execute("cd " + unzipDir.getPath() + " && jar -cvfM0 ../" + config.getTargetJar() + buf.toString());

    }


}
